package com.example.testservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class CountService extends Service {
	private boolean threadDisabled = false;
	private int count;
	
	private ServiceBinder sb = new ServiceBinder();
	public class ServiceBinder extends Binder implements ICountService{

		@Override
		public int getCount() {
			return count;
		}
		
	}

	@Override
	public IBinder onBind(Intent intent) {
		count = intent.getIntExtra("start", 0);
		System.out.println("onBind");
		return sb;
	}
	
	

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		count = intent.getIntExtra("start", 0);
		System.out.println("onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}



	@Override
	public void onCreate() {
		super.onCreate();
		new Thread(){

			@Override
			public void run() {
				while(!threadDisabled) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
					}
					count ++;
					System.out.println("count = " + count);
				}
			}
			
		}.start();
	}

	@Override
	public void onDestroy() {
		threadDisabled = true;
		System.out.println("CountService destroy");
		super.onDestroy();
	}
	
	public int getCount() {
		return count;
	}

	public interface ICountService{
		public abstract int getCount();
	}
}
